﻿/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF WIKIX.
WIKIX IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

WIKIX IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH WIKIX. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using EPiServer.Common;
using EPiServer.Common.Security;
using EPiServer.Common.Tags;
using EPiServer.Core;
using EPiServer.Web;

namespace EPiServer.Research.Wiki.Core
{
    /// <summary>
    /// Contains extension methods for PageData, TemplatePage
    /// </summary>
    public static class WikiExtensions
    {
        public static PageData GetWikiRoot(this PageData pd)
        {
            return WikiManager.Instance.GetWikiRoot(pd.PageLink);
        }


        //#region EntityTagCollection extensions
        ///// <summary>
        ///// Goes throug the tags and compares to the supplied collection and removes
        ///// the tags that is not in the supplied tags, and adds the tags that is in
        ///// supplied tags but not in the origin
        ///// </summary>
        ///// <param name="tags">The tags to go through</param>
        ///// <param name="suppliedTags">The tags to match against</param>
        ///// <param name="user">The user that is author of the added tags</param>
        //public static void SelectiveAdd(this EntityTagCollection tags, TagCollection suppliedTags, IUser user)
        //{
        //    // Go through all tags and remove the tags that are no longer part of the collection
        //    List<string> tagsToRemove = new List<string>();

        //    foreach (EntityTag entityTags in tags)
        //    {
        //        if (!suppliedTags.Contains(entityTags.Tag))
        //        {
        //            tagsToRemove.Add(entityTags.Tag.Name);
        //        }
        //    }

        //    foreach (string tagToRemove in tagsToRemove)
        //    {
        //        tags.RemoveTag(tagToRemove);
        //    }

        //    // Add the new tags
        //    foreach (Tag suppliedTag in suppliedTags)
        //    {
        //        if (!tags.ContainsTag(suppliedTag.Name))
        //        {
        //            tags.Add(new EntityTag(suppliedTag, new UserAuthor(user)));
        //        }
        //    }
        //}
        //#endregion

        //#region Tag extensions
        ///// <summary>
        ///// Gets the url for the users MyPage
        ///// </summary>
        ///// <returns>The uri to the users MyPage</returns>
        //public static Uri GetTagsUri(this ITag tag)
        //{
        //    UrlBuilder url = new UrlBuilder(WikiPageBase.TagsPage.LinkURL);
        //    url.QueryCollection.Add("tag", tag.Name);

        //    if (UrlRewriteProvider.IsFurlEnabled)
        //    {
        //        Global.UrlRewriteProvider.ConvertToExternal(url, WikiPageBase.TagsPage.PageLink, Encoding.UTF8);
        //    }

        //    return url.Uri;
        //}
        //#endregion


        ///// <summary>
        ///// Formats content text for html output
        ///// </summary>
        ///// <param name="text">The text to format</param>
        ///// <returns>The formated text</returns>
        //public static string FormatContentText(this string text)
        //{
        //    if (String.IsNullOrEmpty(text))
        //    {
        //        return String.Empty;
        //    }

        //    return HttpUtility.HtmlEncode(text).Replace("\n", "\n<br/>");
        //}
    }
}
